var exceptionIsRoll = null;

var trunoverDuty = { //页面总变量
  //获取周转量和拖期尾数
  getTrunoverDutyMantissa: function (workShopIdList, hasAlert) {
    return ajaxRequest('displayView/getTrunoverDutyMantissa', {
      token: token,
      autoLogin: "autoLogin",
      workShopIdList: workShopIdList.length === 0 ? '' : JSON.stringify(workShopIdList)
    }, true, hasAlert).then(function (returnData) {
      var startDay = $("#startDay").val();
      var endDay = $("#endDay").val();
      if (/^[-]?[0-9]+(\.?[0-9]+)?$/.test(startDay) == false) {
        startDay = '';
      }
      if (/^[-]?[0-9]+(\.?[0-9]+)?$/.test(endDay) == false) {
        endDay = '';
      }

      var data = returnData.productionList;
      var html = "";

      var html = "<tbody>";
      if (returnData.productionList.length == 1 && (returnData.putOutList === undefined || returnData.putOutList.length <= 0) && (returnData
          .purchaseList === undefined || returnData.purchaseList.length <= 0)) {
        html = "<thead>";
      }

      for (var i = 0; i < data.length; i++) {
        var sumTrunover = 0; //周转量总和
        var delayOrderCount = 0; //拖期单数
        var dutyOrderCount = 0; //尾数单数
        var workShopThml = ""; //车间的html
        var orderList = data[i].orderList;
        for (var j = 0; j < orderList.length; j++) {
          if (startDay != '' && orderList[j].delayTime < startDay) {
            continue;
          }
          if (endDay != '' && orderList[j].delayTime > endDay) {
            continue;
          }
          var matiColor = '';
          var dalyColor = '';
          var is = "否";
          if (orderList[j].isMantissa == 1) {
            is = "是";
            matiColor = 'red';
          }
          if (orderList[j].delayTime > 0) {
            dalyColor = 'red';
          }
          workShopThml += "<tr>" +
            "<td>" + data[i].workShopName + "</td>" +
            "<td>" + orderList[j].companyOrderId + "</td>" +
            "<td>" + orderList[j].factoryId + "</td>" +
            "<td>" + orderList[j].customerId + "</td>" +
            "<td>" + orderList[j].setTime + "</td>" +
            "<td>" + orderList[j].inTime + "</td>" +
            "<td>" + orderList[j].inCount + "</td>" +
            "<td>" + orderList[j].outCount + "</td>" +
            "<td>" + orderList[j].turnover + "</td>" +
            "<td style='color:" + matiColor + "'>" + is + "</td>" +
            "<td style='color:" + dalyColor + "'>" + orderList[j].delayTime + "</td>" +
            "</tr>";
          sumTrunover = sumTrunover + orderList[j].turnover; //总周转量
          if (orderList[j].delayTime > 0) {
            delayOrderCount++; //拖期
          }
          if (orderList[j].isMantissa == 1) {
            dutyOrderCount++; //尾数
          }
        }
        var workshopTitle =
          `<tr style='background-color:#8B8989;'><td colspan='11' >车间名称：${data[i].workShopName} &nbsp;&nbsp;&nbsp; 周转量：${sumTrunover} &nbsp;&nbsp;&nbsp;  尾数单数：${dutyOrderCount} &nbsp;&nbsp;&nbsp;  拖期单数：${delayOrderCount}</td></tr>
        		 <tr style='background-color:#8B6969;'><td>车间名称</td><td>订单编号</td><td>工厂型号</td><td>客户型号</td><td>人工排期</td><td>入库时间</td><td>入库数量</td><td>出库数量</td><td>周转量</td><td>是否有尾数</td><td>拖期天数</td></tr>`;
        if (returnData.productionList.length == 1 && (returnData.putOutList === undefined || returnData.putOutList.length <= 0) && (returnData
            .purchaseList === undefined || returnData.purchaseList.length <= 0)) {
          workshopTitle =
            `<tr style='background-color:#8B8989;'><td colspan='11' >车间名称：${data[i].workShopName} &nbsp;&nbsp;&nbsp; 周转量：${sumTrunover} &nbsp;&nbsp;&nbsp;  尾数单数：${dutyOrderCount} &nbsp;&nbsp;&nbsp;  拖期单数：${delayOrderCount}</td></tr>
            			 <tr style='background-color:#8B6969;'><td>车间名称</td><td>订单编号</td><td>工厂型号</td><td>客户型号</td><td>人工排期</td><td>入库时间</td><td>入库数量</td><td>出库数量</td><td>周转量</td><td>是否有尾数</td><td>拖期天数</td></tr></thead><tbody>`;
        }
        html += workshopTitle;
        html += workShopThml;
      }
      var putOutHtml = "";
      //有查看外协权限
      if (returnData.putOutList !== undefined) {
        var putOutList = returnData.putOutList;
        var putOutHtml =
          "<tr style='background-color:#8B6969;'><td>类型</td><td>订单编号</td><td>工厂型号</td><td>客户型号</td><td>供应商</td><td>制单时间</td><td>流程周期</td><td>外协数量</td><td>返回数量</td><td>欠数</td><td>拖期天数</td></tr>";
        var sumCount = 0.0;
        for (var i = 0; i < putOutList.length; i++) {
          var chaCount = putOutList[i].count - putOutList[i].returnCount;
          sumCount = numAdd(sumCount, chaCount);
          var xx = putOutList[i].createTime;
          if (xx == null) {
            xx = '';
          }

          if (startDay != '' && putOutList[i].delayTime < startDay) {
            continue;
          }
          if (endDay != '' && putOutList[i].delayTime > endDay) {
            continue;
          }
          putOutHtml += "<tr>" +
            "<td>外协</td>" +
            "<td>" + putOutList[i].companyOrderId + "</td>" +
            "<td>" + putOutList[i].factoryId + "</td>" +
            "<td>" + putOutList[i].coustomId + "</td>" +
            "<td>" + putOutList[i].supplierName + "</td>" +
            "<td>" + xx.substring(0, 11) + "</td>" +
            "<td>" + putOutList[i].flowCycle + "</td>" +
            "<td>" + putOutList[i].count + "</td>" +
            "<td>" + putOutList[i].returnCount + "</td>" +
            "<td style='color:red'>" + chaCount + "</td>" +
            "<td style='color:red'>" + putOutList[i].delayTime + "</td>" +
            "</tr>";
        }
        if (putOutList.length > 0) {
          html += "<tr style='background-color:#8B8989;'><th colspan='11' >类型：外协     &nbsp;&nbsp;&nbsp;       未返回数：" + sumCount +
            " &nbsp;&nbsp;&nbsp;    拖期单数：" + putOutList.length + "</th></tr>";
          html += putOutHtml;
        }
      }


      var purchaseHtml = "";
      //有查看采购权限
      if (returnData.purchaseList !== undefined) {
        var purchaseList = returnData.purchaseList;
        var purchaseHtml =
          "<tr style='background-color:#8B6969;'><td>类型</td><td>订单编号</td><td>工厂型号</td><td>客户型号</td><td>制单时间</td><td>最大复期</td><td>采购数量</td><td>入库数量</td><td>欠数</td><td>拖期天数</td><td>采购人</td></tr>";
        var sumCount = 0.0;

        for (var i = 0; i < purchaseList.length; i++) {
          var laterPhase = "";
          if (purchaseList[i].laterPhaseFour != '' && purchaseList[i].laterPhaseFour != null) {
            laterPhase = purchaseList[i].laterPhaseFour;
          } else if (purchaseList[i].laterPhaseThr != '' && purchaseList[i].laterPhaseThr != null) {
            laterPhase = purchaseList[i].laterPhaseThr;
          } else if (purchaseList[i].laterPhaseTwo != '' && purchaseList[i].laterPhaseTwo != null) {
            laterPhase = purchaseList[i].laterPhaseTwo;
          } else if (purchaseList[i].laterPhaseOne != '' && purchaseList[i].laterPhaseOne != null) {
            laterPhase = purchaseList[i].laterPhaseOne;
          }
          if (laterPhase != '') {
            laterPhase = laterPhase.substring(0, 11);
          }
          var chaCount = purchaseList[i].purchaseCount - purchaseList[i].inCount;
          sumCount = numAdd(sumCount, chaCount);
          var xx = purchaseList[i].createTime;
          if (xx == null) {
            xx = '';
          }
          if (startDay != '' && (-purchaseList[i].tardiness) < startDay) {
            continue;
          }
          if (endDay != '' && (-purchaseList[i].tardiness) > endDay) {
            continue;
          }


          purchaseHtml += "<tr>" +
            "<td>采购</td>" +
            "<td>" + purchaseList[i].companyOrderId + "</td>" +
            "<td>" + purchaseList[i].factoryNo + "</td>" +
            "<td>" + purchaseList[i].articleNo + "</td>" +
            "<td>" + xx.substring(0, 11) + "</td>" +
            "<td>" + laterPhase + "</td>" +
            "<td>" + purchaseList[i].purchaseCount + "</td>" +
            "<td>" + purchaseList[i].inCount + "</td>" +
            "<td style='color:red'>" + chaCount + "</td>" +
            "<td style='color:red'>" + (-purchaseList[i].tardiness) + "</td>" +
            "<td>" + purchaseList[i].purchasePeople + "</td>" +
            "</tr>";
        }
        if (purchaseList.length > 0) {
          sumCount = numAdd(sumCount, chaCount);
          html += "<tr style='background-color:#8B8989;'><th colspan='11' >类型：采购     &nbsp;&nbsp;&nbsp;       欠数：" + sumCount +
            " &nbsp;&nbsp;&nbsp;    拖期单数：" + purchaseList.length + "</th></tr>";
          html += purchaseHtml;
        }
      }
      html += "</tbody>";
      $("#xx").html(html);

      trunoverDuty.rollTable();
      page.rollTable();
      return returnData;
    });
  },
  rollTable: function () {
    //已经在滚动了,就不再滚动
    var displayHeight = 0;
    var bodyHeight = 0;
    displayHeight = $(window).height();
    bodyHeight = $("body").height();
    //如果表格高度大于div的高度，则需要轮播
    if (bodyHeight > displayHeight) {
      if (exceptionIsRoll != null) {
        return false;
      }
      exceptionIsRoll = setInterval("trunoverDuty.tableScroll()", 4000);
    } else {
      if (exceptionIsRoll != null) {
        clearInterval(exceptionIsRoll);
        exceptionIsRoll = null;
      }
    }
    return true;
  },
  tableScroll: function () {
    $("#xx tbody").append($("#xx tbody tr:first"));
  }
}


function numAdd(num1, num2) {
  var baseNum, baseNum1, baseNum2;
  try {
    baseNum1 = num1.toString().split(".")[1].length;
  } catch (e) {
    baseNum1 = 0;
  }
  try {
    baseNum2 = num2.toString().split(".")[1].length;
  } catch (e) {
    baseNum2 = 0;
  }
  baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
  return (num1 * baseNum + num2 * baseNum) / baseNum;
};
